package com.example.meituan.activity.user.adapter;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.example.meituan.R;
import com.example.meituan.activity.bean.BusinessBean;
import com.example.meituan.activity.bean.ImgBean;
import com.example.meituan.activity.bean.OrderBean;

import com.example.meituan.activity.bean.OrderDetailsBean;
import com.example.meituan.activity.bean.ShopBean;
import com.example.meituan.activity.bean.ShopCarBean;
import com.example.meituan.activity.bean.TypeBean;
import com.example.meituan.activity.user.OrderItemClickListener;
import com.example.meituan.activity.user.UserOrderManageActivity;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.ImgDao;
import com.example.meituan.dao.OrderDetailsDao;
import com.example.meituan.dao.ShopCarDao;
import com.example.meituan.dao.ShopDao;
import com.example.meituan.dao.TypeDao;

import java.io.File;
import java.util.List;

public class UserOrderAdapter extends RecyclerView.Adapter<UserOrderAdapter.UserOrderViewHolder>{

    private List<OrderBean> orderList;
    private static OrderItemClickListener listener;
    public UserOrderAdapter(List<OrderBean>orderBeans , OrderItemClickListener listener) {
        this.orderList =orderBeans;
        this.listener=listener;
    }
    @NonNull
    @Override
    public UserOrderAdapter.UserOrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_order_list, parent, false);
        return new UserOrderAdapter.UserOrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UserOrderAdapter.UserOrderViewHolder holder, int position) {
        OrderBean orderBean= orderList.get(position);
        holder.bind(orderBean);
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }

    public static class UserOrderViewHolder extends RecyclerView.ViewHolder {
        private TextView orderName, orderShopName, orderType, orderTotalPrice, orderNum, cancleOrder, confirmOrder;
        private ImageView orderShopImg, orderStore;

        public UserOrderViewHolder(@NonNull View itemView) {
            super(itemView);
            orderName = itemView.findViewById(R.id.order_name);
            orderShopName = itemView.findViewById(R.id.order_shop_name_user);
            orderType = itemView.findViewById(R.id.order_type);
            orderTotalPrice = itemView.findViewById(R.id.order_total_price);
            orderNum = itemView.findViewById(R.id.order_product_num);
            orderShopImg = itemView.findViewById(R.id.order_shop_img);
            orderStore = itemView.findViewById(R.id.order_to_store);
            cancleOrder = itemView.findViewById(R.id.cancle_order);
            confirmOrder = itemView.findViewById(R.id.confirm_order);
        }

        public void bind(OrderBean orderBean) {
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onOrderItemClick(orderBean);
                }
            });
            OrderDetailsBean orderDetailsBean = OrderDetailsDao.getOrderDetailById(orderBean.getsOrderDetialsId());
            BusinessBean businessBean = AdminDao.getBusiness(orderDetailsBean.getShopId());
            orderNum.setText("X" + orderDetailsBean.getNum());
            orderName.setText(orderDetailsBean.getName());
            orderType.setText(orderDetailsBean.getDetailType());
            orderTotalPrice.setText("￥" + orderDetailsBean.getPrice());
            orderShopName.setText(businessBean.getsName());
            ImgBean firstImgBean = ImgDao.getImageById(orderDetailsBean.getImg());
            String imagePath = firstImgBean.getImg();
            File imageFile = new File(imagePath);
            if (imageFile.exists()) {

                Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                if (bitmap != null) {
                    orderShopImg.setImageBitmap(bitmap);
                }

            }
            orderStore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.toStore(orderBean);
                    }
                }
            });
            cancleOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.cancleOrder(orderBean);
                    }
                }
            });
            confirmOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.confirmOrder(orderBean);
                    }

                }

            });
        }
    }
}
